package com.bank.controller;

import com.bank.pojo.Transaction;
import com.bank.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/transactions")
public class TransactionController {

    private final TransactionService transactionService;

    @Autowired
    public TransactionController(TransactionService transactionService) {
        this.transactionService = transactionService;
    }

    @GetMapping("/{number}")
    public ResponseEntity<List<Transaction>> getTransactionsByUser(@PathVariable String number) {
        try {
            List<Transaction> transactions = transactionService.findByUserNumber(number);
            return ResponseEntity.ok(transactions);
        } catch (Exception e) {
            // Log the exception
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }



    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, String>> deleteTransaction(@PathVariable Integer id) {
        try {
            boolean isDeleted = transactionService.deleteTransactionById(id);
            Map<String, String> response = new HashMap<>();

            if (isDeleted) {
                response.put("message", "交易记录已删除");
                return ResponseEntity.ok(response);
            } else {
                response.put("message", "交易记录未找到");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
            }
        } catch (Exception e) {
            Map<String, String> errorResponse = new HashMap<>();
            errorResponse.put("error", "删除交易记录时发生错误");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

//    @DeleteMapping("/{id}")
//    public ResponseEntity<String> deleteTransaction(@PathVariable Integer id) {
//        try {
//            boolean isDeleted = transactionService.deleteTransactionById(id);
//            if (isDeleted) {
//                return ResponseEntity.ok("交易记录已删除");
//            } else {
//                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("交易记录未找到");
//            }
//        } catch (Exception e) {
//            // Log the exception
//            e.printStackTrace();
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除交易记录时发生错误");
//        }
//    }

    @DeleteMapping("/batchDelete")
    public ResponseEntity<String> batchDelete(@RequestBody List<String> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("没有提供需要删除的交易记录");
            }

            boolean isDeleted = transactionService.batchDelete(ids);
            if (isDeleted) {
                return ResponseEntity.ok("选中的交易记录已删除");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除失败");
            }
        } catch (Exception e) {
            // Log the exception
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除时发生错误");
        }
    }
}

//@RestController
//@RequestMapping("/api/transactions")
//public class TransactionController {
//
//    private final TransactionService transactionService;
//
//    @Autowired
//    public TransactionController(TransactionService transactionService) {
//        this.transactionService = transactionService;
//    }
//
//    // 获取用户的交易记录
//    @GetMapping("/{number}")
//    public ResponseEntity<List<Transaction>> getTransactionsByUser(@PathVariable String number) {
//        List<Transaction> transactions = transactionService.findByUserNumber(number);
//        return ResponseEntity.ok(transactions);
//    }
//
//
//    @DeleteMapping("/{id}")
//    public ResponseEntity<String> deleteTransaction(@PathVariable Integer id) {
//        boolean isDeleted = transactionService.deleteTransactionById(id);
//        if (isDeleted) {
//            return ResponseEntity.ok("交易记录已删除");
//        } else {
//            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("交易记录未找到");
//        }
//    }
//
//    @DeleteMapping("/batchDelete")
//    public ResponseEntity<String> batchDelete(@RequestBody List<String> ids) {
//        if (ids == null || ids.isEmpty()) {
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("没有提供需要删除的交易记录");
//        }
//
//        boolean isDeleted = transactionService.batchDelete(ids);
//        if (isDeleted) {
//            return ResponseEntity.ok("选中的交易记录已删除");
//        } else {
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除失败");
//        }
//    }
//}
//    // 批量删除交易记录的接口
//    @DeleteMapping("/batchDelete")
//    public ResponseEntity<String> batchDelete(@RequestBody List<String> ids) {
//        if (ids == null || ids.isEmpty()) {
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("没有提供需要删除的交易记录");
//        }
//
//        // 执行批量删除
//        boolean isDeleted = transactionService.batchDelete(ids);
//
//        if (isDeleted) {
//            return ResponseEntity.ok("选中的交易记录已删除");
//        } else {
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除失败");
//        }
//    }
//
//     // 删除单个交易记录
//    @DeleteMapping("/{id}")
//    public ResponseEntity<String> deleteTransaction(@PathVariable Integer id) {
//        boolean isDeleted = transactionService.deleteTransactionById(id);
//        if (isDeleted) {
//            return ResponseEntity.ok("交易记录已删除");
//        } else {
//            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("交易记录未找到");
//        }
//    }


//
//        @DeleteMapping("/batchDelete")
//        public ResponseEntity<String> batchDelete(@RequestBody Map<String, Object> payload) {
//            // 获取 ids 数组
//            List<String> ids = (List<String>) payload.get("ids");
//
//            if (ids == null || ids.isEmpty()) {
//                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("没有提供需要删除的记录");
//            }
//
//            // 批量删除逻辑
//            boolean isDeleted = transactionService.batchDelete(ids);
//
//            if (isDeleted) {
//                return ResponseEntity.ok("选中的交易记录已删除");
//            } else {
//                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除失败");
//            }
//        }



